<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\StaffsModel;
use Illuminate\Support\Facades\DB;

class StaffsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $staffs = StaffsModel::get();
        return view('staffs.index', ['staffs' => $staffs]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('staffs.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, StaffsModel $staff)
    {
        // $data = $request->post();
        $staff->name = $request->name;
        $staff->sex =  $request->sex;
        $staff->age = $request->age;
        $staff->salary =  $request->salary;
        $staff->email = $request->email;
        $staff->mobile =  $request->mobile;
        $staff->password = sha1($request->password);
        $staff->save();
        return redirect(route('staffs.index'));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $staff = StaffsModel::where('staff_id', $id)->first();
        return view('staffs.show', ['staff' => $staff]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $staff = StaffsModel::where('staff_id', $id)->first();
        return view('staffs.edit', ['staff' => $staff]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        // $data = $request->post();
        $data['name'] = $request->name;
        $data['sex'] =   $request->sex;
        $data['age'] =  $request->age;
        $data['salary'] =   $request->salary;
        $data['email'] =  $request->email;
        $data['mobile'] =   $request->mobile;
        $data['password'] =  $request->password;
        StaffsModel::where('staff_id', $id)->update($data);
        return redirect(route('staffs.index'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        // StaffsModel::where('staff_id', $id)->delete();
        return redirect(route('staffs.index'));
    }
}
